package edu.njfu.sas.servlet;

import com.google.gson.Gson;
import edu.njfu.sas.dao.StudentDao;
import edu.njfu.sas.dao.impl.StudentDaoImpl;
import edu.njfu.sas.model.Student;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/deleteStudent")
public class DeleteStudentServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
        PrintWriter writer=response.getWriter();
        Map map=new HashMap();
        String  stuno=request.getParameter("stuno");
       StudentDao studentDao=new StudentDaoImpl();
        boolean result= false;
        try {
            result = studentDao.deleteStudent("stuno");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if(result){
            map.put("result","ok");
            map.put("msg","删除成功！");
        }else{
            map.put("result","false");
            map.put("msg","删除失败！");
        }
        writer.write(new Gson().toJson(map));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}
